package org.example.completed.controller;

import org.example.completed.domain.*;
import org.example.completed.model.vo.ResultVO;
import org.example.completed.service.AdoptionApplicationService;
import org.example.completed.service.AdoptionCloudRecordService;
import org.example.completed.service.PetService;
import org.example.completed.service.UserService;
import org.example.completed.utils.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  领养申请 AdoptionApplicationController
 *
 * @since 2025-04-07
 */
@RestController
@RequestMapping("/adoptionApplication")
public class AdoptionApplicationController {
    @Autowired
    private AdoptionApplicationService adoptionApplicationService;
    @Autowired
    private UserService userService;
    @Autowired
    private PetService petService;
    @Autowired
    private AdoptionCloudRecordService adoptionCloudRecordService;

    /**
     * 获取所有领养申请列表
     * @return 领养申请列表
     */
    @GetMapping("/list")
    public ResultVO<List<AdoptionApplication>> getAllApplications() {
        List<AdoptionApplication> applications = adoptionApplicationService.list();
        return ResultVO.ok(applications);
    }

    /**
     * 提交领养申请
     * @param application 领养申请实体
     * @return 操作结果
     */
    @Transactional
    @PostMapping("/add")
    public ResultVO<?> addApplication(@RequestBody AdoptionApplication application) {
        String userId = application.getUserId();
        String petId = application.getPetId();
        try {
            // 检查userId是否存在
            User user = userService.getById(userId);
            if (user == null){
                return ResultVO.fail("用户不存在");
            }

            // 获取宠物信息
            Pets pet = petService.getById(petId);
            if (pet == null) {
                return ResultVO.fail("未找到对应宠物信息");
            }

            // 检查宠物库存是否足够
            if (pet.getStock() <= 0) {
                return ResultVO.fail("该宠物库存不足，无法提交领养申请");
            }
            pet.setStock(pet.getStock() - 1);
            petService.updateById(pet);

            // 保存领养申请
            AdoptionApplication adoptionApplication = BeanCopyUtil.copyObject(application, AdoptionApplication.class);
            adoptionApplication.setAvatarUrl(user.getImg());
            adoptionApplication.setUserAcoount(user.getUserAcoount());
            adoptionApplication.setPetName(pet.getName());

            adoptionApplicationService.save(adoptionApplication);
            return ResultVO.ok();
        } catch (Exception e) {
            return ResultVO.fail("数据库操作出错：" + e.getMessage());
        }
    }

    /**
     * 更新领养申请状态
     * @return 操作结果
     */
    @Transactional
    @PostMapping("/updateStatus")
    public ResultVO<?> updateStatus(@RequestBody AdoptionApplication dto) {
        AdoptionApplication adoptionApplication = adoptionApplicationService.getById(dto.getId());
        boolean flag=true;
        String isCloud = adoptionApplication.getIsCloud();
        String status = dto.getStatus();
        if (isCloud.equals("2") && status.equals("1")){
            User user = userService.getById(adoptionApplication.getUserId());
            Pets pet = petService.getById(adoptionApplication.getPetId());
            AdoptionCloudRecordDO cloudRecordDO = BeanCopyUtil.copyObject(adoptionApplication, AdoptionCloudRecordDO.class);
            cloudRecordDO.setId(dto.getId());
            cloudRecordDO.setPetName(pet.getName());
            cloudRecordDO.setUserAcoount(user.getUserAcoount());
            cloudRecordDO.setAvatarUrl(user.getImg());

            flag = adoptionCloudRecordService.save(cloudRecordDO);
        }

        adoptionApplication.setStatus(status);
        flag = adoptionApplicationService.updateById(adoptionApplication);
        if (!flag)
            return ResultVO.fail("更新失败");
        else
            return ResultVO.ok();
    }

    /**
     * 根据用户ID查询领养申请
     * @param userId 用户ID
     * @return 领养申请列表
     */
    @GetMapping("/getByUserId")
    public ResultVO<List<AdoptionApplication>> getApplicationsByUserId(@RequestParam String userId) {
        List<AdoptionApplication> applications = adoptionApplicationService.getApplicationsByUserId(userId);
        return ResultVO.ok(applications);
    }

    /**
     * 根据宠物ID查询领养申请
     * @param petId 宠物ID
     * @return 领养申请列表
     */
    @GetMapping("/getByPetId")
    public ResultVO<List<AdoptionApplication>> getApplicationsByPetId(@RequestParam String petId) {
        List<AdoptionApplication> applications = adoptionApplicationService.getApplicationsByPetId(petId);
        return ResultVO.ok(applications);
    }


}